package com.baozhun.mall.common.widget.loading

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import androidx.databinding.DataBindingUtil
import com.baozhun.mall.common.R
import com.baozhun.mall.common.databinding.ViewCustomPageLoadBinding
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.Glide

/**
 * @Title：页面loadingview
 * @Description：首次进入加载默认图
 * @Author：xuefeng.zhu
 * @Since：2023/12/18
 * @Version：1.0.0
 */
class CustomPageLoadView(context: Context, private var attrs: AttributeSet? = null) : LinearLayout(context, attrs) {
    private var mBinding: ViewCustomPageLoadBinding? = null

    init {
        if (!isInEditMode) {
            orientation = VERTICAL
            mBinding = DataBindingUtil.bind(View.inflate(context, R.layout.view_custom_page_load, null))
            Glide.with(context).load(R.drawable.loading).into(mBinding!!.ivLoadView)
            var lp = LayoutParams(ScreenUtils.getScreenWidth(), ScreenUtils.getScreenHeight())
            //获取上间距
            val typedArray: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.PageLoadingView, 0, 0)
            var marginTop = typedArray.getResourceId(R.styleable.PageLoadingView_loading_view_margin_top, 320)
            (mBinding!!.ivLoadView.layoutParams as MarginLayoutParams).setMargins(0, marginTop, 0, 0)
            typedArray.recycle()
            addView(mBinding?.root, lp)
        }
    }

    /**
     * 隐藏占位view
     */
    fun hidePlaceholderView() {
        mBinding?.llLoadLayout?.visibility = View.GONE
    }

    /**
     * 显示占位view
     */
    fun showPlaceholderView() {
        mBinding?.llLoadLayout?.visibility = View.VISIBLE
    }

    /**
     * 显示占位view
     */
    fun showPlaceholderView(marginTop: Int) {
        (mBinding?.ivLoadView?.layoutParams as MarginLayoutParams).setMargins(0, SizeUtils.dp2px(marginTop.toFloat()), 0, 0)
    }

}